const path = require('path');
const wechat = require('think-wechat')
const isDev = think.env === 'development';

module.exports = [
  {
    handle: 'meta',
    options: {
      logRequest: isDev,
      sendResponseTime: isDev
    }
  },
  {
    handle: 'resource',
    enable: true,
    options: {
      root: path.join(think.ROOT_PATH, 'www'),
      publicPath: /^\/(assets|static|sales|home|upload|favicon\.ico|robots\.txt|MP_verify_EaUhBmDY0BSJDj4R\.txt)/
    }
  },
{
    handle: 'trace',
    enable: !think.isCli,
    options: {
      debug: isDev,
    templates: path.join(think.ROOT_PATH, 'view/error')
    }
},
   {
    handle: wechat,
    match: '/app/wxmp/wechat',
    options: async() => {
      await think.timeout(1000);
      return {
        token: think.config('setup.weixinToken'),
        appid: think.config('setup.weixinAppid'),
        encodingAESKey: '',
        checkSignature: false // 可选，默认为true。由于微信公众平台接口调试工具在明文模式下不发送签名，所以如要使用该测试工具，请将其设置为false
      };
    }
  },
  {
    handle: 'payload',
    options: {}
  },
  {
    handle: 'router',
    options: {
    	optimizeHomepageRouter: false
    }
  },
  {
		handle: 'pjax',
		options: {},
		match: (ctx) => {

			if(ctx.headers['X-PJAX'.toLowerCase()] == 'true') {
				return true;
			}
			if(ctx.querystring && ctx.querystring.indexOf('_pjax=') != -1) {
				return true;
			}
			return false;

		}
	},
  'logic',
  'controller'
];
